package imdbfolder.workers;

import imdbfolder.communications.HttpRequester;
import imdbfolder.data.Movie;
import imdbfolder.data.MovieSelectionMap;
import jregex.Matcher;
import jregex.Pattern;

public class MovieInfoFetcher extends MyWorker<Movie, Void> {

    private final Movie movie;

    public MovieInfoFetcher(Movie movie) {
        this.movie = movie;
    }

    @Override
    public Movie onWork() throws Exception {
        //System.out.println("Fetcher running!");

        HttpRequester requester = new HttpRequester();

        String html = requester.getHttpResult(movie.getImdbUrl());

        matchInfoToMovie(html);

        return movie;
    }

    public void matchInfoToMovie(String html) {
        Matcher matcher;

        matcher = new Pattern("tt\\d{7}").matcher(html);
        if (matcher.find()) {
            movie.setImdbId(matcher.group(0));
        }

        matcher = new Pattern("<span itemprop=\"ratingValue\">([0-9\\.]+)</span>").matcher(html);
        if (matcher.find()) {
            movie.setScore(Float.parseFloat(matcher.group(1)));
        }

        matcher = new Pattern("([0-9,]+) votes").matcher(html);
        if (matcher.find()) {
            movie.setNumVotes(matcher.group(1));
        }

        matcher = new Pattern("<title>(.*) \\(.*?(\\d{4})(/I*)?\\).*?</title>").matcher(html);

        if (matcher.find()) {
            movie.setTitle(matcher.group(1));
            movie.setYear(Integer.parseInt(matcher.group(2)));
        }
        
        matcher = new Pattern("itemprop=\"genre\"\\s*>(.+?)<", Pattern.DOTALL).matcher(html);

        movie.setGenres("");
        int x = 0;
        while (matcher.find()) {
            if (x++ > 0) {
                movie.setGenres(movie.getGenres() + ", ");
            }
            movie.setGenres(movie.getGenres() + matcher.group(1).trim());
        }
        
        matcher = new Pattern("itemprop=\"director\"\\s*>(.+?)<", Pattern.DOTALL).matcher(html);

        movie.setDirectors("");
        int x1 = 0;
        while (matcher.find()) {
            if (x1++ > 0) {
                movie.setDirectors(movie.getDirectors() + ", ");
            }
            movie.setDirectors(movie.getDirectors() + matcher.group(1).trim());
        }
        
        // Pattern to fetch "stars" (~3) only
        // matcher = new Pattern("itemprop=\"actors\"\\s+>(.+?)<", Pattern.DOTALL).matcher(html);
 
        // Pattern to match complete cast
        // <a onclick="(new Image()).src='/rg/tt-cast/cast-1/images/b.gif?link=%2Fname%2Fnm0000206%2F';" href="/name/nm0000206/">Keanu Reeves</a>
        matcher = new Pattern("\\/rg\\/tt-cast\\/cast-\\d{1,2}\\/images\\/b\\.gif\\?link\\=\\%2Fname\\%2Fnm\\d{7}\\%2F\\'\\;\"\\s*href\\=\"\\/name\\/nm\\d{7}\\/\"\\s*>([^<]+?)</a>").matcher(html);

        movie.setActors("");
        int x2 = 0;
        while (matcher.find()) {
            if (x2++ > 0) {
                movie.setActors(movie.getActors() + ", ");
            }
            movie.setActors(movie.getActors() + matcher.group(1).trim());
        }
        
        
        matcher = new Pattern("([0-9]+) min</time>").matcher(html);
        if (matcher.find()) {
            movie.setRuntime(Integer.parseInt(matcher.group(1)));
        }

        matcher = new Pattern("<h2>Storyline</h2>\\s*<p>\\s*(.*?)<em", Pattern.DOTALL).matcher(html);
        if (matcher.find()) {
            movie.setPlotSummary(matcher.group(1).trim());
        }

        matcher = new Pattern("><img src=\"([^\"]*)\"[^\\<]*itemprop\\=\"image\"").matcher(html);
        if (matcher.find()) {
        // 	System.out.println(matcher.group(1));
            MovieSelectionMap.getInstance().storeImage(matcher.group(1), movie);
        }

        movie.setFetchedInfo(true);
    }
}
